package cc.telecomdigital.tdstock.view;

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

public class TextFitTextView extends TextView
{
	private static final String	TAG	= TextFitTextView.class.getSimpleName();

	public TextFitTextView(Context context)
	{
		super(context);
		initialise();
	}

	public TextFitTextView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initialise();
	}

	private void initialise()
	{
		testPaint = new Paint();
		testPaint.set(this.getPaint());
		//max size defaults to the intially specified text size unless it is too small
		maxTextSize = this.getTextSize();
		if (maxTextSize < 11)
		{
			maxTextSize = 20;
		}
		minTextSize = 10;
		// TDStockLog.d(TAG, "init the fontsize to fit the label!  and its maxTextSize  is "  +   maxTextSize);
	}

	/* Re size the font so the specified text fits in the text box
	 * assuming the text box is the specified width.
	 */
	private void refitText(String text, int textWidth)
	{
		if (text.length() <= 5)
		{
			this.setTextSize(TypedValue.COMPLEX_UNIT_SP, maxTextSize);
			//    		this.postInvalidate();
		}
		else if (text.length() == 6)
		{
			this.setTextSize(TypedValue.COMPLEX_UNIT_SP, maxTextSize - 3);
			//    		this.postInvalidate();
		}
		else if (text.length() == 7)
		{
			this.setTextSize(TypedValue.COMPLEX_UNIT_SP, maxTextSize - 4);
			//    		this.postInvalidate();
		}
		else
		{
			this.setTextSize(TypedValue.COMPLEX_UNIT_SP, minTextSize);
			//    		this.postInvalidate();
		}
		/*if(text == null) {
			return;
		}
		 TDStockLog.d(TAG, "init the fontsize to fit the label!  and its textWidth  is "  +   textWidth + ", its text is " +text );
		if (textWidth > 0) {
		   int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
		   float trySize = maxTextSize;
		       if(testPaint == null) {
		       	 testPaint = this.getPaint();
		       	 
		       	 TDStockLog.d(TAG, "create  a new paint!");
		       }
		       if(testPaint == null) {
		       	testPaint = new Paint();
		      }
		//               testPaint.setTextSize(trySize); 
		       setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
		//            testPaint.setTextSize(TypedValue.COMPLEX_UNIT_PX, (int)trySize);

		   while ((trySize > minTextSize) && (testPaint.measureText(text) > availableWidth)) {
		       trySize -= 1;
		       if (trySize <= minTextSize) {
		           trySize = minTextSize;
		           break;
		       }
		//                testPaint.setTextSize(trySize);
		       setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
		//                setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);

		   }
		   setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
		 //  this.setTextSize(trySize);
		   
		   TDStockLog.d(TAG, "aujst the fontsize to fit the label!  and its size  is "  +   trySize);
		}*/
	}

	@Override
	protected void onTextChanged(final CharSequence text, final int start,
			final int before, final int after)
	{
		//    	TDStockLog.d(TAG, "ready to aujst the fontsize to fit the label!");
		refitText(text.toString(), getWidth());
	}

	//    @Override
	//    protected void onSizeChanged (int w, int h, int oldw, int oldh) {
	//        if (w != oldw) {
	//        	TDStockLog.d(TAG, "ready to aujst the fontsize to fit the label!");
	//            refitText(this.getText().toString(), oldw);
	//        }
	//    }

	//    @Override
	//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	//    {
	//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	//        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
	////        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
	//        refitText(this.getText().toString(), parentWidth);
	////        this.setMeasuredDimension(parentWidth, parentHeight);
	////    	TDStockLog.d(TAG, "onMeasure:" + parentWidth + "," +  parentHeight);
	//    }

	//Getters and Setters
	public float getMinTextSize()
	{
		return minTextSize;
	}

	public void setMinTextSize(int minTextSize)
	{
		this.minTextSize = minTextSize;
	}

	public float getMaxTextSize()
	{
		return maxTextSize;
	}

	public void setMaxTextSize(int minTextSize)
	{
		this.maxTextSize = minTextSize;
	}

	//Attributes
	private Paint	testPaint;
	private float	minTextSize;
	private float	maxTextSize;

}
